package com.rpc.test.test;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rpc.utils.ExceptionUtils;

/*********
 *
 *
 *@author 197
 *@date 2020/6/30 18:21
 *
 **/


public class FeatureMain {


  private static final Logger logger = LoggerFactory.getLogger(FeatureMain.class);
  static <T> Supplier<T> wrapped(Callable<T> callable) {
    return () -> {
      try {
        return callable.call();
      } catch (RuntimeException e1) {
        throw e1;
      } catch (Throwable e2) {
        throw new RuntimeException(e2);
      }
    };
  }

  public static void main(String[] args) {
//    CompletableFuture.supplyAsync(() -> {
//      int i = 1 / 0;
//      return null;
//    }).exceptionally(
//        e -> {
//          return "xxx";
//        }
//    ).thenAccept((r) -> {
//      System.out.println(r);
//    });
    CompletableFuture.supplyAsync(() -> {
      String s = null;
      try {
        s = CompletableFuture.supplyAsync(() -> {
          try {
            Thread.sleep(1);
          } catch (Exception e) {
            logger.error("exception:{} stackTrace:{}", e.getMessage(), ExceptionUtils.getTrace(e));
          }
          int i = 1 / 0;
          return "zc";
        }).get(3L, TimeUnit.SECONDS);
      } catch (InterruptedException e) {
        return "InterruptedException";
      } catch (ExecutionException e) {
        return "ExecutionException";
      } catch (TimeoutException e) {
        return "TimeoutException";
      }
      return s;
    }).exceptionally(Throwable::getMessage).thenAccept(System.out::println);
    System.out.println("xxxxxxxxxxxxxxxxxxxxxx");

//    CompletableFuture.supplyAsync(()->"").
  }
}
